package com.molyfun.parents.activity.report;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseFileDownloadActivity;
import com.molyfun.parents.activity.report.adapter.PlanDetailTabAdapter;
import com.molyfun.parents.activity.report.iepPlan.IEPPlanView;
import com.molyfun.parents.impl.PlanDetailTabCallback;
import com.molyfun.parents.impl.ScrollViewListener;
import com.molyfun.parents.rest.bean.GetEvaluateAnalysisListResponse;
import com.molyfun.parents.rest.bean.GetIepPlanTableResponse;
import com.molyfun.parents.rest.bean.GetReportResultResponse;
import com.molyfun.parents.rest.bean.PlanDetailResponse;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.view.FileListView;
import com.molyfun.parents.view.ObservableScrollView;
import com.molyfun.parents.view.TwoTabBar;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ObjectAnimator;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 评估结果页面
 */
public class ReportResultActivity extends BaseFileDownloadActivity implements View.OnClickListener, TwoTabBar.TwoBarClickListener, PlanDetailTabCallback {

    private static final String TIME_FLAG = "time_str";

    @InjectView(R.id.base_title_left)
    ImageView mTitleBack;
    @InjectView(R.id.base_title)
    TextView mBaseTitle;

    @InjectView(R.id.evaluate_detail_file_layout)
    FileListView mEvaluateFileLayout;


    @InjectView(R.id.recyclerview_second_menu)
    RecyclerView mRecyclerViewLeft;
    @InjectView(R.id.analyze_list_layout)
    LinearLayout mAnalyzeTableLayout;

    @InjectView(R.id.iep_plan_view)
    IEPPlanView mIEPPlanView;

    @InjectView(R.id.iep_layout)
    LinearLayout mIepLayout;

    @InjectView(R.id.two_tab_bar)
    TwoTabBar mTwoTabBar;
    @InjectView(R.id.tv_file_type)
    TextView tvFileType;

    @InjectView(R.id.scrollview)
    ObservableScrollView mScrollView;

    private String mReportId;
    private String mMsgId;
    private List<PlanDetailResponse.PlanDetailBean.AnalysislistBean> mTab1List;
    private List<PlanDetailResponse.PlanDetailBean.AnalysislistBean> mTab2List;


    private PlanDetailTabAdapter mPlanDetailTabAdapter;
    LinearLayoutManager mLinearLayoutManager;

    public static void startActivity(Context context, String reportId) {
        Intent intent = new Intent();
        intent.putExtra(Const.REPORT_ID, reportId);
        intent.setClass(context, ReportResultActivity.class);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, String reportId, String mMsgId) {
        Intent intent = new Intent();
        intent.putExtra(Const.REPORT_ID, reportId);
        intent.putExtra(Const.MSG_ID, mMsgId);
        intent.setClass(context, ReportResultActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_result);
        ButterKnife.inject(this);

        mBaseTitle.setText("评估详情");

        mTitleBack.setOnClickListener(this);

        getSupportActionBar().setTitle("");

        mReportId = getIntent().getStringExtra(Const.REPORT_ID);
        mMsgId = getIntent().getStringExtra(Const.MSG_ID);
        mTwoTabBar.initData("分析结果", "IEP计划");
        mTwoTabBar.setTwoBarClickListener(this);

        mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        mRecyclerViewLeft.setLayoutManager(mLinearLayoutManager);
        mRecyclerViewLeft.setHasFixedSize(true);

        mPlanDetailTabAdapter = new PlanDetailTabAdapter(this, this);
        mRecyclerViewLeft.setAdapter(mPlanDetailTabAdapter);

        getReportResult(mReportId);
        getReportTable(mReportId);
        getEvaluateAnalysisDetail(mReportId);

        mScrollView.setScrollViewListener(new CommonScrollViewListener());
        mIEPPlanView.setListScrollListener(new CommonScrollViewListener());

    }

    public class CommonScrollViewListener implements ScrollViewListener {
        @Override
        public void onScrollChanged(ObservableScrollView observableScrollView, int l, int t, int oldl, int oldt) {
            System.out.println("----l>" + l);
            System.out.println("----t>" + t);
            if (oldt < t && ((t - oldt) > 15)) {// 向上

                ObjectAnimator animator = ObjectAnimator.ofFloat(mIepLayout, "translationY", 0, mIepLayout.getHeight());
                animator.setDuration(500);
                animator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mIepLayout.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                animator.start();

            } else if (oldt > t && (oldt - t) > 15) {// 向下
//                    Log.d("TAG"," 向下滑动")
                ObjectAnimator animator = ObjectAnimator.ofFloat(mIepLayout, "translationY", mIepLayout.getHeight(), 0);
                animator.setDuration(500);
                animator.start();
                mIepLayout.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onTwoBarClick(int chooseTab) {
        switch (chooseTab) {
            case TwoTabBar.TwoTabChooseTab.BAR_ONE:
                mIEPPlanView.setVisibility(View.GONE);
//                mIepLayout.setVisibility(View.VISIBLE);
                mAnalyzeTableLayout.setVisibility(View.VISIBLE);
                mRecyclerViewLeft.setVisibility(View.VISIBLE);
                loadFileListLayout(mTab1List);
                tvFileType.setText("分析结果文件");
                break;
            case TwoTabBar.TwoTabChooseTab.BAR_TWO:
                mIEPPlanView.setVisibility(View.VISIBLE);
//                mIepLayout.setVisibility(View.VISIBLE);
                mAnalyzeTableLayout.setVisibility(View.GONE);
                mRecyclerViewLeft.setVisibility(View.GONE);
                loadFileListLayout(mTab2List);
                tvFileType.setText("IEP文件");
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
        }
    }

    @Override
    public void onPlanDetailTabClick(int position) {
        if (analysisList != null) {
            showAnalysisTab(analysisList.get(position));
        }
    }

    private void showAnalysisTab(GetReportResultResponse.ReportDetailTableCell reportDetailTableCell) {
        mAnalyzeTableLayout.removeAllViews();
        LinearLayout tableLayout = (LinearLayout) LayoutInflater.from(ReportResultActivity.this).inflate(R.layout.layout_report_analyze_table_cell, null, false);
        ((TextView) tableLayout.findViewById(R.id.analyze_table_age)).setText("" + reportDetailTableCell.getBeginvalue() + "~" + reportDetailTableCell.getEndvalue() + "月");
        ((TextView) tableLayout.findViewById(R.id.analyze_table_abilitystatus)).setText(reportDetailTableCell.getAbilitystatus());
        ((TextView) tableLayout.findViewById(R.id.analyze_table_adanalysis)).setText(reportDetailTableCell.getAdanalysis());
        LinearLayout suggestingLayout = (LinearLayout) tableLayout.findViewById(R.id.suggestion_layout);

        if (reportDetailTableCell.getEdusuggestionlist() != null && reportDetailTableCell.getEdusuggestionlist().size() > 0) {
            for (GetReportResultResponse.EduSuggestionBean eduSuggestionBean : reportDetailTableCell.getEdusuggestionlist()) {
                View tableCellLayout = LayoutInflater.from(this).inflate(R.layout.view_evaluate_analyze_table_cell, null, false);
                ((TextView) tableCellLayout.findViewById(R.id.evaluate_table_cell_title)).setText(eduSuggestionBean.getAgeRange());
                ((TextView) tableCellLayout.findViewById(R.id.evaluate_table_cell_content)).setText(eduSuggestionBean.getContent());
                suggestingLayout.addView(tableCellLayout);
            }
        }

        mAnalyzeTableLayout.addView(tableLayout);
    }

    private List<GetReportResultResponse.ReportDetailTableCell> analysisList;

    private void getReportResult(String reportId) {

        showProgressDialog();
        Call<GetReportResultResponse> call = MFPApplication.getInstance().getRestClient().getEvaluateanalysisResult(reportId);
        call.enqueue(new Callback<GetReportResultResponse>() {
            @Override
            public void onResponse(Call<GetReportResultResponse> call, Response<GetReportResultResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {

                    if (response.body().getData() != null && response.body().getData().size() > 0) {
                        analysisList = response.body().getData();
                        mPlanDetailTabAdapter.refreshData(response.body().getData());
                        showAnalysisTab(response.body().getData().get(0));
                    }

                }
            }

            @Override
            public void onFailure(Call<GetReportResultResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

    private void getReportTable(String reportId) {

        showProgressDialog();
        Call<GetIepPlanTableResponse> call = MFPApplication.getInstance().getRestClient().getEvaluatePlanList(reportId);
        call.enqueue(new Callback<GetIepPlanTableResponse>() {
            @Override
            public void onResponse(Call<GetIepPlanTableResponse> call, Response<GetIepPlanTableResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    if (response.body().getData() != null && response.body().getData().size() > 0) {
                        GetIepPlanTableResponse.IEPPlanTableBean iepPlanTableBean = response.body().getData().get(0);

                        mIEPPlanView.initData(iepPlanTableBean);
                        //文件列表
                        mTab2List = iepPlanTableBean.getEvalauateieplist();
//                        loadFileListLayout(mTab2List);

                    }

                }
            }

            @Override
            public void onFailure(Call<GetIepPlanTableResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

    public void getEvaluateAnalysisDetail(String reportId) {
        Call<GetEvaluateAnalysisListResponse> call = MFPApplication.getInstance()
                .getRestClient().getEvaluateAnalysisList(reportId);
        call.enqueue(new Callback<GetEvaluateAnalysisListResponse>() {
            @Override
            public void onResponse(Call<GetEvaluateAnalysisListResponse> call,
                                   Response<GetEvaluateAnalysisListResponse> response) {
//                toast("成功");
                //文件列表
                mTab1List = response.body().getData();
                loadFileListLayout(mTab1List);
            }

            @Override
            public void onFailure(Call<GetEvaluateAnalysisListResponse> call, Throwable t) {

            }
        });
    }

    private void loadFileListLayout(List<PlanDetailResponse.PlanDetailBean.AnalysislistBean> list) {
//        if (list != null && list.size() > 0) {
            mEvaluateFileLayout.initDataList(list);
//            mEvaluateFileLayout.removeAllViews();
//            for (final PlanDetailResponse.PlanDetailBean.AnalysislistBean analysislistBean : list) {
//                LinearLayout fileLayout = (LinearLayout) LayoutInflater.from(ReportResultActivity.this).inflate(R.layout.layout_report_analyze_file_cell, null, false);
//                ((TextView) fileLayout.findViewById(R.id.file_name)).setText(analysislistBean.getName());
//                fileLayout.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//
//                        if((analysislistBean.getName().toLowerCase()).trim().contains("jpg")|| (analysislistBean.getName().toLowerCase()).trim().contains("png")) {
//                            ImageViewerActivity.startActivity(ReportResultActivity.this,analysislistBean.getUrl());
//                        }else {
//                            GameViewerActivity.startActivity(ReportResultActivity.this, RestClient.ONLINE_OFFICE_FILE_URL_HEAD + analysislistBean.getUrl());
////                            downloadFeil(analysislistBean.getUrl(), analysislistBean.getName());
//                        }
//                    }
//                });
//                mEvaluateFileLayout.addView(fileLayout);
//            }
//        }

    }

}
